#!/bin/sh

mount -o rw,loop=/dev/loop1 ./initrd /mnt
rm -rf /mnt/lib/modules/*
BASEPATH=/lib/modules/`uname -r`
mkdir -p /mnt/$BASEPATH
cp -p $BASEPATH/modules.* /mnt$BASEPATH/

for MOD in $(lsmod | awk '{ if ( NR > 1 ) {print $1 }}')
do
    MODPATH=$(modprobe -l | grep "/$MOD.")
    DESTPATH=$(dirname $MODPATH)
    mkdir -p /mnt$DESTPATH
    cp -p $MODPATH /mnt$MODPATH
done

sync
umount /mnt
